// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_gray");
    var top = require("../top/top_white");
    var nature = $("#nature").val();
	var enterpriseName;
	var businessLicence;
	var address;
	var contactAddress;
	var businessScope;
	var registeredCapital;
	var businessTimeLimit;
	var legalPerson;
	var legalIdcard;
	var legalPeriod;
	var depositBank;
	var accountHolder;
	var creditCard;
	var businessType;
	var enterpriseType;
	var	belongsEnterpriseCode;
	var proxySaleCode;
	var codeUSC;
	var certificate;
	var procurator;
	var procuratorIdcard;
	var procuratorPeriod;
	var flag = false;
	var province;
	var city;
	var district;
	var authenticationId;
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
    
    var init = function(){
    	if(base.getData("sid")==null){
    		window.location="../login/login.html";
    	}
    	
    	authenticationId = base.getUrlParam("enterpriseId");
    	top.init("企业资质认证修改");
    	footer.init();
    	$("#head").removeClass("none");
    	$(".login").addClass("none");
    	$("#main").removeClass("none");
    	$("#footer").removeClass("none");
    	router.init();
    	inquireEnterpriseDetail();//查询企业详情
		initEvent(); 
    };
    
	var initEvent = function(){
		
		//调显示与隐藏函数
		hideSpan();
		hideCardType();
		$("#main").on("change", "#sUserType", function(){
			hideSpan();
		});
		$("#main").on("change", "#scardType", function(){
			hideCardType();
		});
		
		$("#main").on("change", "#creditCard", function(){
			var bn = tools.getBankName($(this).val());
			if (bn == ""){
				alert("未知开户行！");
			} else {
				$("#depositBank").val(bn);
			}
		});
		
		$("#main").on("change", "#legalIdcard", function(){
			var promptString = validate.isIdCard($(this), "请输入正确的身份证号码!");
			if ("" != promptString) {
				base.alert(2, promptString)
			} 
		});
		
		layer.tips(tools.formatCurrency($("input[name=registeredCapital]").val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
		  tips: [1, '#3595CC'],
		  time: 3000
		});
		
		$("#main").on("focus", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$("#main").on("change", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$('.bankInfo').card({
            container: $('.card-wrapper'),
            formSelectors: {
            	numberInput: "input[name=creditCard]",
            	nameInput: 'input[name="accountHolder"]',
			    expiryInput: 'input[name="expiry"]',
			    cvcInput: 'input[name="cvc"]'
            }
        });
		//认证提交
		$("#main").on("click", ".next", function(){
			databind.submitDom($("#main"), function(data){//获取div的id值通过HTML中的action="informationCheck"传到后台
//				history.go(-1);
				if(authenticationId!=null){
					tools.cancelProtectDoubleClick();
	                if (data.status == 200) {
						router.route("approve/certificates_upload.html?authenticationId="+authenticationId);
	                } else {
	                }
				}else{
						tools.cancelProtectDoubleClick();
		                if (data.status == 200) {
							router.route("approve/certificates_upload.html?authenticationId="+data.datas[0].authenticationId);
							addOrganize();
		                } else {
		             }
	             }
		  	});
		});
		
//		单位类型 nature 
//		企业名 enterpriseName 
//		单位注册号 businessLicence 
//		营业执照所在地 address
//		联系地址 contactAddress
//		经营范围 businessScope
//		注册资金 registeredCapital
//		营业期限 businessTimeLimit
//		法定代表人姓名 legalPerson
//		身份证号 legalIdcard
//		证件有效期 legalPeriod
//		开户行 depositBank
//		开户名称 accountHolder
//		银行卡号 creditCard

		databind.fillInput1($("#main"), {action: "inquireCurrentEnterpriseDetail.json"}, function(data){
			$("select[name=city]").html("<option>"+data.datas[0].city+"</option>");
			$("select[name=district]").html("<option>"+data.datas[0].district+"</option>");
		});

    };

    //查询企业详细信息
	var inquireEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].enterpriseGradeEvaluationDetail != null) {
						databind.fillForm($("#main"), data.datas[0]);
					}
				} else {
					base.alert(2, data.message);
				}
				//判断认证状态
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication != 3) { //未认证
						$(".return").hide();
					}
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication == 3) { //已认证
						$(".next").hide();
					}
				//调显示与隐藏函数
				hideSpan();
		        hideCardType();
			}
		});
		databind.enableInput("#main");
	};
    
    //注册企业资质
    var createEnterprise = function(){
		base.fnAjax({
			action: "createEnterprise.json",
			async: false,
			oData: {
				proxySaleCode: proxySaleCode,
                nature: nature,
                enterpriseName: enterpriseName,
                businessLicence: businessLicence,
                address: address,
                contactAddress: contactAddress,
                businessScope: businessScope,
                registeredCapital: registeredCapital,
                businessTimeLimit: businessTimeLimit,
                businessType: businessType,
                legalPerson: legalPerson,
                legalIdcard: legalIdcard,
                legalPeriod: legalPeriod,
                depositBank: depositBank,
                accountHolder: accountHolder,
                creditCard: creditCard,
                enterpriseType: enterpriseType,
				belongsEnterpriseCode: belongsEnterpriseCode,
				province: province,
				district: district,
				city: city,
				codeUSC: codeUSC,
				certificate: certificate,
				procurator: procurator,
				procuratorIdcard: procuratorIdcard,
				procuratorPeriod: procuratorPeriod
            }, //传递参数
            fnSuccess: function(data) {
            	tools.cancelProtectDoubleClick();
                if (data.status == 200) {
					router.route("approve/certificates_upload.html?enterpriseId="+data.datas[0].enterpriseId);
					addOrganize();
                } else {
                }
            }
		});
	};
	
	 //注册企业资质
    var updateEnterprise = function(){
		base.fnAjax({
			action: "updateEnterprise.json",
			async: false,
			oData: {
				enterpriseId: enterpriseId,
				proxySaleCode: proxySaleCode,
                nature: nature,
                enterpriseName: enterpriseName,
                businessLicence: businessLicence,
                address: address,
                contactAddress: contactAddress,
                businessScope: businessScope,
                registeredCapital: registeredCapital,
                businessTimeLimit: businessTimeLimit,
                businessType: businessType,
                legalPerson: legalPerson,
                legalIdcard: legalIdcard,
                legalPeriod: legalPeriod,
                depositBank: depositBank,
                accountHolder: accountHolder,
                creditCard: creditCard,
                enterpriseType: enterpriseType,
				belongsEnterpriseCode: belongsEnterpriseCode,
				province: province,
				district: district,
				city: city,
				codeUSC: codeUSC,
				certificate: certificate,
				procurator: procurator,
				procuratorIdcard: procuratorIdcard,
				procuratorPeriod: procuratorPeriod
            }, //传递参数
            fnSuccess: function(data) {
            	tools.cancelProtectDoubleClick();
                if (data.status == 200) {
					router.route("approve/certificates_upload.html?enterpriseId="+enterpriseId);
                } else {
                }
            }
		});
	};
	
		//联动1
	var hideSpan = function() {
		var sUserType = $("#sUserType").val();
		if(sUserType == "以代理人身份注册") {
			$("#hide1").show();
			$("#hide2").show();
			$("#procurator").attr('isNull',1);
			$("#procuratorIdcard").attr('isNull',1);
			$("#procuratorPeriod").attr('isNull',1);
		}
		if(sUserType == "以法定代表人身份注册") {
			$("#hide2").hide();
			$("#hide1").show();
			$("#procurator").attr('isNull',0);
			$("#procuratorIdcard").attr('isNull',0);
			$("#procuratorPeriod").attr('isNull',0);
		}
	};
	
	//联动2
	var hideCardType = function() {
		var scardType = $("#scardType").val();
		if(scardType == "0") {
			$("#hideRegNo").show();
			$("#certificateShow").show();
			$("#codeUSCHide").hide();
			$("#codeUSC").attr('isNull',0);
			$("#regNo").attr('isNull',1);
			$("#certificate").attr('isNull',1);
		}
		if(scardType == "1") {
			$("#hideRegNo").hide();
			$("#certificateShow").hide();
			$("#codeUSCHide").show();
			$("#codeUSC").attr('isNull',1);
			$("#regNo").attr('isNull',0);
			$("#certificate").attr('isNull',0);
		}
	};
	
	//连接e签宝生成证书,返回账户唯一标识
    var addOrganize = function(){
		base.fnAjax({
			action: "addOrganize.json",
			async: false,
			oData: {
            }, //传递参数
            fnSuccess: function(data) {
            }
		});
	};
	
    return {
        init:init
    };
});